package com.kuaidu.ll.quickreadnews.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import butterknife.ButterKnife;

/**
 * Created by liulei on 2017/7/6.
 * 二次封装baseActivity
 */

public abstract class BaseViewActivity extends BaseActivity {

    protected FragmentManager mFragmentManager;

    @Override
    protected void beforeInit(Bundle savedInstanceState) {
        super.beforeInit(savedInstanceState);
        ButterKnife.bind(this);
    }

    public void showToast(String message) {
        showToast(message, Toast.LENGTH_SHORT);
    }

    public void showToast(int id) {
        String message = this.getBaseContext().getResources().getString(id);
        showToast(message);
    }

    public void showToast(String message, int length) {
        Toast.makeText(this.getBaseContext(), message, length).show();
    }

    public void showToast(int id, int length) {
        String message = this.getBaseContext().getResources().getString(id);
        showToast(message, length);
    }


    public Fragment loadFragment(Context context,int layout,Fragment fragment){
        if (mFragmentManager == null){
            mFragmentManager = getSupportFragmentManager();
        }

        return fragment;
    }

    public Fragment loadFragment(Context context,int layoutId,String className){
        return loadFragment(context,layoutId,className,null);
    }

    public Fragment loadFragment(Context context,int layout,String className,Bundle bundle){
        return loadFragment(context,layout,className,bundle,false);
    }

    public Fragment loadFragment(Context context, int layoutId, String className, Bundle bundle, boolean isAddToBackStack) {
        if (mFragmentManager == null){
            mFragmentManager = getSupportFragmentManager();
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        Fragment fragment = mFragmentManager.findFragmentByTag(className);
        if (fragment == null){
            fragment = Fragment.instantiate(context,className,bundle);
            fragmentTransaction.add(layoutId,fragment,className);
        }
        else {
            if (!fragment.isAdded()){
                fragmentTransaction.add(layoutId,fragment,className);
            }
            else {
                fragmentTransaction.show(fragment);
            }
            if (isAddToBackStack){
                fragmentTransaction.addToBackStack(className);
            }
            if (!isFinishing()){
//                这个方法会丢失状态  掩盖错误  不到万不得已不使用
                fragmentTransaction.commitAllowingStateLoss();
                fragmentTransaction.commit();
                mFragmentManager.executePendingTransactions();
            }
        }
        return fragment;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
